/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.kj.entity.KjServiceType;
import com.jeesite.modules.kj.enums.IntegralEnum;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.service.KjServiceTypeService;
import com.jeesite.modules.kj.utils.IntegralUtil;
import com.jeesite.modules.kj.utils.SystemLogUtil;
import com.jeesite.modules.sys.entity.Log;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.kj.entity.KjIntegralAuth;
import com.jeesite.modules.kj.service.KjIntegralAuthService;

import java.math.BigDecimal;
import java.util.List;

/**
 * kj_integral_authController
 * @author hzy
 * @version 2021-07-13
 */
@Controller
@RequestMapping(value = "${adminPath}/kj/kjIntegralAuth")
public class KjIntegralAuthController extends BaseController {

	@Autowired
	private KjIntegralAuthService kjIntegralAuthService;

	@Autowired
	private KjServiceTypeService kjServiceTypeService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public KjIntegralAuth get(String id, boolean isNewRecord) {
		return kjIntegralAuthService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("kj:kjIntegralAuth:view")
	@RequestMapping(value = {"list", ""})
	public String list(KjIntegralAuth kjIntegralAuth, Model model) {
		model.addAttribute("kjIntegralAuth", kjIntegralAuth);
		List<KjServiceType> serviceTypeList = kjServiceTypeService.findList(new KjServiceType());
		model.addAttribute("serviceTypeList", serviceTypeList);
		return "modules/kj/kjIntegralAuthList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("kj:kjIntegralAuth:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<KjIntegralAuth> listData(KjIntegralAuth kjIntegralAuth, HttpServletRequest request, HttpServletResponse response) {
		kjIntegralAuth.setPage(new Page<>(request, response));
		Page<KjIntegralAuth> page = kjIntegralAuthService.findPage(kjIntegralAuth);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("kj:kjIntegralAuth:view")
	@RequestMapping(value = "form")
	public String form(KjIntegralAuth kjIntegralAuth, Model model) {
		model.addAttribute("kjIntegralAuth", kjIntegralAuth);
		return "modules/kj/kjIntegralAuthForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("kj:kjIntegralAuth:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated KjIntegralAuth kjIntegralAuth, HttpServletRequest request) {
		KjIntegralAuth oldAuth = kjIntegralAuthService.get(kjIntegralAuth.getId());
		if(!KjIntegralAuth.SENDED.equals(kjIntegralAuth.getSendFlag()) && KjIntegralAuth.OK.equals(kjIntegralAuth.getApproveStatus())) {
			KjMember kjMember = kjIntegralAuth.getKjMember();
			//认证时长不计入总时长
			IntegralUtil.addIntegrals(kjMember, kjIntegralAuth.getId(), kjIntegralAuth.getJobTypeId(), IntegralEnum.AUTH, kjIntegralAuth.getIntegrals(), kjIntegralAuth.getTotalHour(), kjIntegralAuth.getServiceName());
			//IntegralUtil.addIntegrals(kjMember, kjIntegralAuth.getId(), kjIntegralAuth.getJobTypeId(), IntegralEnum.AUTH, kjIntegralAuth.getIntegrals(), BigDecimal.ZERO, kjIntegralAuth.getServiceName());
			kjIntegralAuth.setSendFlag(KjIntegralAuth.SENDED);
			SystemLogUtil.insertAdminOperate(UserUtils.getUser(), "服务认证", Log.TYPE_UPDATE, "认证通过", kjMember.getName() + "提交的" + "\"" + kjIntegralAuth.getServiceName() + "\"", request);
			kjIntegralAuthService.auth(kjIntegralAuth);
		}else if(!KjIntegralAuth.REJECT.equals(oldAuth.getApproveStatus()) && KjIntegralAuth.REJECT.equals(kjIntegralAuth.getApproveStatus())){
			kjIntegralAuthService.auth(kjIntegralAuth);
			SystemLogUtil.insertAdminOperate(UserUtils.getUser(), "服务认证", Log.TYPE_UPDATE, "认证拒绝", kjIntegralAuth.getKjMember().getName() + "提交的" + "\"" + kjIntegralAuth.getServiceName() + "\"", request);
		}
		kjIntegralAuthService.save(kjIntegralAuth);
		return renderResult(Global.TRUE, text("保存服务认证成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("kj:kjIntegralAuth:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(KjIntegralAuth kjIntegralAuth) {
		kjIntegralAuthService.delete(kjIntegralAuth);
		return renderResult(Global.TRUE, text("删除服务认证成功！"));
	}

	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("kj:kjIntegralAuth:view")
	@RequestMapping(value = "kjIntegralAuthSelect")
	public String empUserSelect(KjIntegralAuth kjIntegralAuth, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("KjIntegralAuth", kjIntegralAuth);
		return "modules/kj/kjIntegralAuthSelect";
	}


	/**
	 * 导出认证数据
	 */
	@RequiresPermissions("kj:kjIntegralAuth:view")
	@RequestMapping(value = "exportData")
	public void exportData(KjIntegralAuth kjIntegralAuth, Boolean isAll, String ctrlPermi, HttpServletResponse response) {

		kjIntegralAuth.setPageNo(null);
		kjIntegralAuth.setPageSize(null);
		List<KjIntegralAuth> list = kjIntegralAuthService.findList(kjIntegralAuth);
		String fileName = "认证数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("认证数据", KjIntegralAuth.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}
}
